The value of a final
or const
variable can’t be changed. In Dart const
declares a compile-time constant,
which will be computed during compilation and this might improve performance. So in general it is recommended to use const
, where
possible. In Dart it is also possible to use const
to create constant values, as well as to declare constructors that create constant
values. final
declarations initialized with such values should be const
.
Noncompliant code example
final i = 1000; // Noncompliant
final ints = const [1, 2, 3]; // Noncompliant
class MyClass {
static final name = "NAME"; // Noncompliant
}
Compliant solution
const i = 1000;
const ints = [1, 2, 3]; // const after assignment can be omitted
class MyClass {
static const name = "NAME";
}